/**
 * COPYRIGHT. Harry Wu 2011. ALL RIGHTS RESERVED.
 * Project: just-core
 * Author: Harry Wu <harrywu304@gmail.com>
 * Created On: Aug 17, 2011 7:16:30 PM
 *
*/ 
package org.justframework.core.router;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * router factory
 *
 */
public class RouterFactory {
	
	/**
	 * logger
	 */
	private static Logger logger = LoggerFactory.getLogger(RouterFactory.class);
	
	/**
	 * get router by policy name
	 * @param policy
	 * @return
	 */
	public static Router getRouter(String policy){
		Policy py = Policy.valueOf(policy.toUpperCase());
		Router router;
		try {
			if(Policy.NULL != py){
				router = (Router)py.klass.newInstance();
				return router;
			}	
		} catch (InstantiationException e) {
			logger.error("getRouter fail",e);
		} catch (IllegalAccessException e) {
			logger.error("getRouter fail",e);
		}
		return null;
	}
	
	/**
	 * route policy
	 * @author whx
	 *
	 */
	private enum Policy {
		RANDOM(RandomRouter.class),
		MOD(ModRouter.class),
		KETAMA(KetamaRouter.class),
		MASTER_SLAVE(MasterSlaveRouter.class),
		NULL(Void.class);
		
		/**
		 * policy implementation class
		 */
		private Class klass;
		
		private Policy(Class klass){
			this.klass = klass;
		}
	};
	

}
